import pymysql

ZSBFIELDS = ['采集时间', 'A相电压', 'B相电压', 'C相电压', '出口压力', '前轴承温度', '后轴承温度', '定子中部温度', '定子前部温度', '定子后部温度', '无功功率', '有功功率', '泵前轴温度', '泵后轴温度', '润滑油压力', '电机冷却水压力', '瞬时流量', '进口压力', 'A相电流量', 'B相电流量', 'C相电流量', '地下油箱液位', '泵平衡压力', '泵润滑油压力', '电机润滑油压力', '高架油箱液位']


def connect(sscursor=False):
    if sscursor:
        con = pymysql.connect(host='localhost', user='master', password='123456', database='zhushuizhan', cursorclass = pymysql.cursors.SSCursor)
    else:
        con = pymysql.connect(host='localhost', user='master', password='123456', database='zhushuizhan')
    return con


def getDeviceList(con):
    '''
    获取所有注水泵表的表名
    :param con: connection 数据库连接
    :return: list 注水泵表明列表
    '''
    cur = con.cursor()
    cur.execute("select * from device_info")
    data = cur.fetchall()
    data = list(map(lambda x: {'table_name':x[0],'device_name':x[1]}, data))
    cur.close()
    return data

def getConfigThresh(scon,tableName):
    cur = scon.cursor()
    cur.execute("select config from config_thresh where table_name='%s';" % tableName)
    data=cur.fetchone()[0]
    cur.close()
    return data

def saveConfigThresh(scon,tableName,configText):
    cur = scon.cursor()
    cur.execute('''update config_thresh set config="%s" where table_name='%s';''' % (configText,tableName))
    cur.close()
    scon.commit()



def getZSBOneRow(cur):
    '''
    获取注水泵表的下一条记录
    '''
    data = cur.fetchone()
    if data is None:
        return None
    data=data[:-1]
    data = {ZSBFIELDS[index]:value for index,value in enumerate(data)}
    return data


if __name__ == '__main__':
    con = connect()
    zsbNameList = getDeviceList(con)
    cur=con.cursor()
    cur.execute("select * from %s"%zsbNameList[0]['table_name'])
    row=getZSBOneRow(cur)
    print(row)
    con.close()
